/**
 * @file: service.js
 * @description:
 * @author: YUWENJING
 * @date: 2020/2/17
 * @contact:wenjingyuer@126.com
 */
import Taro from '@tarojs/taro'
import {redirectWithAuth, toast} from "../../../utils";
import {getClassByClassKey} from "../../../service/index";
import {getGlobalData, setGlobalData} from "../../../global.d";
import {getUserById} from "../../../service";
import Envs from '../../../constant/envs'
import {AUTH_LEVEL} from "../../auth/authFilter";

const db = Taro.cloud.database();
const _ = db.command

export function updateClassAdmin(classId, classAdmins) {

   Taro.cloud.callFunction({
     name:'updateClassAdmin',
     data:{
       classId,
       classAdmins,
       env:Envs.current
     }
   })
}
export function updateClassStudent(classId, classStudents) {
  Taro.cloud.callFunction({
    name:'updateClassStudent',
    data:{
      classId,
      classStudents,
      env:Envs.current
    }
  })
}
export async function deleteUserClass(userId, classKey) {
  let user = await getUserById(userId)
  let {userClass,userLastTimeClass} = user
  if(userLastTimeClass.classKey === classKey){
    userLastTimeClass = {
      className:'【默认加入】酷班级功能展示一班',
      classKey:'00000001',
      type:'user'
    }
  }
  userClass = userClass.filter((item)=>{
    return item.classKey !== classKey
  })

  Taro.cloud.callFunction({
    name:'deleteUserClass',
    data:{
      env:Envs.current,
      userId,
      userClass,
      userLastTimeClass
    }
  })

  toast('操作成功')
}
export async function changeUserType(userId, classKey, type) {
  let user = await getUserById(userId)
  let {userClass,userLastTimeClass} = user
  if(userLastTimeClass.classKey === classKey) {
    userLastTimeClass.type = type
  }
  userClass = userClass.map((item,index)=>{
    if(item.classKey === classKey ){
      item.type = type
    }
    return item
  })
  Taro.cloud.callFunction({
    name:'deleteUserClass',
    data:{
      env:Envs.current,
      userId,
      userClass,
      userLastTimeClass
    }
  })

  toast('操作成功')


}
